﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class RechargeController : BaseController<recharge>
    {
        //
        // GET: /Recharge/  
        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<recharge> bll
        {
            get
            {
                return new RechargeBll();
            }

        }

        //定义一个class  提供给前台数据  总的充值

        public class ChargeAllModel  
        {
            public string userName;
            public decimal rechargeAllMoney;//充值总金额
            public int rechargeNum;   //充值次数
            public string wechatName;
            public string wechatCode;
            public string userPhone;
            public decimal balance;
        }
            //单次充值记录
        public class ChargeOneModel
        {
             public int rechargeId;   
            public string rechargeDate;
            public decimal? rechargeValue;

        }
        UserBll userbll = new UserBll();

        MemberBll memberBll = new MemberBll();

        //查询充值信息记录
        public ActionResult GetChargeNote(int page, int rows, string sort, string adminUsername, string userPhone, string order)//order asc desc 
        {
            var listAllNoteModel = new List<ChargeAllModel>();
            var listAllNoteModelOrder = new List<ChargeAllModel>();
            //查询充值记录表里的所有信息 
            var listNote = bll.Search().GroupBy(x => x.wechatCode).Select(x => new { x.FirstOrDefault().wechatCode, rechargeAllMoney = x.Sum(y => y.rechargeValue), rechargeNum = x.Count(), }).ToList();

            foreach (var oneCode in listNote)
            {
                Expression<Func<user, bool>> condition2 = null;
                //没有查询用户名条件 
                if (adminUsername == "" || adminUsername == null)
                {
                    condition2 = x => x.wechatCode == oneCode.wechatCode;
                }
                ///输入查询条件  用户名
                else
                {
                    condition2 = x => x.wechatCode == oneCode.wechatCode && x.wechatName.Contains(adminUsername);
                }
                //输入 查询手机号
                if (userPhone != "")
                {
                    condition2 = x => x.userPhone.Contains(userPhone) && x.wechatCode == oneCode.wechatCode;
                }
                //两个查询条件都不空
                if (userPhone != "" && adminUsername != "")
                {
                    condition2 = x => x.userPhone.Contains(userPhone) && x.wechatCode == oneCode.wechatCode && x.wechatCode == oneCode.wechatCode;
                }
                //根据用户充值信息记录里的相关信息，查询用户表的相关信息
                var list2 = userbll.Search(condition2).Select(x => new { x.userCode, x.userName, x.wechatCode,x.userPhone, x.wechatName }).ToList();

                if (list2.Count != 0)
                {
                    ChargeAllModel allmodel = new ChargeAllModel();
                    allmodel.userName = list2.FirstOrDefault().userName;
                    allmodel.userPhone = list2.FirstOrDefault().userPhone;
                    allmodel.wechatName = list2.FirstOrDefault().wechatName;
                    allmodel.wechatCode = oneCode.wechatCode;
                    allmodel.rechargeAllMoney = Convert.ToDecimal(oneCode.rechargeAllMoney);
                    allmodel.rechargeNum = oneCode.rechargeNum;
                    allmodel.balance = memberBll.Search(x => x.wechatCode == oneCode.wechatCode).Select(x => new { x.memberBalance }).FirstOrDefault().memberBalance;
                    listAllNoteModel.Add(allmodel);
                }
            }
            if (sort == "rechargeNum" && order == "asc")
            {
                listAllNoteModelOrder = listAllNoteModel.OrderBy(x => x.rechargeNum).ToPagedList(page, rows).ToList();
            }
            if (sort == "rechargeNum" && order == "desc")
            {
                listAllNoteModelOrder = listAllNoteModel.OrderByDescending(x => x.rechargeNum).ToPagedList(page, rows).ToList();
            }
            if (sort == "rechargeAllMoney" && order == "asc")
            {
                listAllNoteModelOrder = listAllNoteModel.OrderBy(x => x.rechargeAllMoney).ToPagedList(page, rows).ToList();
            }
            if (sort == "rechargeAllMoney" && order == "desc")
            {
                listAllNoteModelOrder = listAllNoteModel.OrderByDescending(x => x.rechargeAllMoney).ToPagedList(page, rows).ToList();
            }
            return Json(new { rows = listAllNoteModelOrder, total = listAllNoteModel.Count });
        }

        /// <summary>
        ///  充值记录详情
        /// </summary>
        /// <param name="wechatCode"></param>
        /// <returns></returns>
        public ActionResult GetRechargeDetails(string wechatCode)
        {
            //ToString("yyyy-MM-dd HH-mm-ss") 
            var oneRechargeList=new List<ChargeOneModel>();
            var listNote = bll.Search(x => x.wechatCode == wechatCode).Select(x => new { x.wechatCode, x.rechargeValue, x.rechargeId,  x.rechargeDate}).ToList();
            foreach (var item in listNote)
            {
                ChargeOneModel modelone = new ChargeOneModel();
                modelone.rechargeDate = item.rechargeDate.ToString("yyyy-MM-dd HH:mm:ss");
                modelone.rechargeId = item.rechargeId;
                modelone.rechargeValue = item.rechargeValue;
                oneRechargeList.Add(modelone);
            }
            return Json(oneRechargeList);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult GetMonAll()
        {
            decimal? all=0; 
            var allList = bll.Search().Select(x => new { x.rechargeValue }).ToList();
            foreach (var item in allList)
            {
                all += item.rechargeValue;
            }
            return Json(new { all=all},JsonRequestBehavior.AllowGet);
 
        }

    }
}
